from j264.tools.attributes.Attribute import Attribute


class ArrayAttribute(Attribute):

    ### INITIALIZER ###

    def __init__(self, section=None, length=None, minimum=None, maximum=None):
        Attribute.__init__(self, section)
        assert isinstance(minimum, (int, float, type(None)))
        assert isinstance(maximum, (int, float, type(None)))
        if length is not None:
            length = int(length)
            assert 0 < length
        self._length = length
        if maximum is not None:
            maximum = float(maximum)
        self._maximum = maximum
        if minimum is not None:
            minimum = float(minimum)
        self._minimum = minimum

    ### READ-ONLY PUBLIC ATTRIBUTES ###

    @property
    def length(self):
        return self._length

    @property
    def maximum(self):
        return self._maximum

    @property
    def minimum(self):
        return self._minimum

    ### PUBLIC METHODS ###

    def get_input_massage(self):
        def massage(self, arg):
            return tuple([float(x) for x in arg])
        return massage

    def get_input_predicate(self):
        length = self.length
        maximum = self.maximum
        minimum = self.minimum
        def predicate(self, arg):
            if not isinstance(arg, (list, tuple)):
                return False
            if length is not None and len(arg) != length:
                return False
            for x in arg:
                if not isinstance(x, (int, float)):
                    return False
                if maximum is not None and maximum < x:
                    return False
                if minimum is not None and x < minimum:
                    return False
            return True
        return predicate
                
                
